package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.cache.CourseCache;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.Class;
import com.example.teesystem.entity.WebLog;
import com.example.teesystem.entity.vo.PageResponseVo;
import com.example.teesystem.entityDto.Log;
import com.example.teesystem.entityDto.LogSelect;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.LogService;
import com.example.teesystem.mapper.LogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 12510
* @description 针对表【t_log】的数据库操作Service实现
* @createDate 2022-05-28 12:26:17
*/
@Service
@RequiredArgsConstructor
public class LogServiceImpl extends ServiceImpl<LogMapper, WebLog>
    implements LogService{
    private final LogMapper logMapper;

    public Response add(WebLog log){
        if(logMapper.selectById(log.getId()) == null) {
            return Response.ok(this.save(log));
        } else {
            return Response.error(ResultCode.LOG_PARAMETER_CONTENT_ERROR.getMsg(),ResultCode.LOG_PARAMETER_CONTENT_ERROR.getCode());
        }
    }


    public Response search(LogSelect logSelect) {
        Date start,end;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            start = simpleDateFormat.parse(logSelect.getStartTime());
            end = simpleDateFormat.parse(logSelect.getEndTime());
        }catch (Exception e){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }

        if(!(CourseCache.courseMap.get(logSelect.getCourseId()).getTeacher() == MyHandlerInterceptor.getUid())){
            return Response.error(ResultCode.NO_PERMISSION);
        }

        if(logSelect.getUserId() == null){
            logSelect.setUserId(MyHandlerInterceptor.getUid());
        }
        QueryWrapper<WebLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",logSelect.getUserId())
                .eq("course_id",logSelect.getCourseId())
                .between("service_time",start,end)
                .orderByDesc("service_time");
        Page<WebLog> classPage = new Page<>(logSelect.getCur(), logSelect.getSize());
        Page<WebLog> result = this.page(classPage, wrapper);
        List<WebLog> webLogs = result.getRecords();
        List<Log> logs = new ArrayList<>();
        DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        if(webLogs == null || webLogs.size() == 0) {
            return Response.ok(logs);
        } else {
            for (WebLog webLog : webLogs) {
                Log log = new Log();
                BeanUtils.copyProperties(webLog, log);
                log.setServiceTime(dateformat.format(webLog.getServiceTime()));
                logs.add(log);
            }
        }
        return Response.ok(new PageResponseVo<>(logs,classPage.getCurrent(),classPage.getSize(), classPage.getPages()));
    }
}




